package com.hc.bankonline.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hc.bankonline.services.BankOnlineRecordservices;
import com.hc.bankonline.services.BankOnlineRecordservicesImpl;
import com.sun.org.apache.regexp.internal.RE;

public class BankOnlineSearchServlet extends HttpServlet {

	private static final long serialVersionUID = -2625846284394910592L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		String before = request.getParameter("beforedate");
		Date beforedate = null;
		if (before != null) {
			try {
				beforedate = new SimpleDateFormat("yyyy-MM-dd").parse(before);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		String after = request.getParameter("afterdate");
		Date afterdate = null;
		if (before != null) {
			try {
				afterdate = new SimpleDateFormat("yyyy-MM-dd").parse(after);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}

		String i = request.getParameter("indexPage");
		if (i == null) {
			i = "1";
		}
		int indexPage = Integer.valueOf(i);
		int pageSize = 5;

		Map map = (Map) request.getSession().getAttribute("mapAcount");

		String loginAccount = (String) map.get("loginAccount");

		BankOnlineRecordservices bankOnlineRecordBiz = new BankOnlineRecordservicesImpl();
		List<Map<String, Object>> list = bankOnlineRecordBiz.searchAll(beforedate, afterdate, loginAccount, indexPage, pageSize);

		request.setAttribute("list", list);
		request.setAttribute("beforedate", before);
		request.setAttribute("afterdate", after);
		request.setAttribute("indexPage", indexPage);
		request.setAttribute("totalTable", bankOnlineRecordBiz.findTotalTable(loginAccount));
		request.setAttribute("totalPage", bankOnlineRecordBiz.findTotalPage(loginAccount, indexPage, pageSize));

		request.getRequestDispatcher("select.jsp").forward(request, response);

	}
}
